package com.qyhl.webtv.module_news.utils;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;

/**
 * Created by Administrator on 2018/4/20.
 */

public class RecyclerViewScrollUtil {
    public static final int TYPE_START = 1;
    public static final int TYPE_CENTER = 2;
    public static final int TYPE_END = 3;
    private int TYPE = TYPE_CENTER;

    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private LinearInterpolator linearInterpolator;
    private ScrollListener listener;
    private int position = -1;

    private int checkPosition = -1;

    private boolean isScroll = false;
    private boolean isCheckScroll = false;

    public RecyclerViewScrollUtil(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        linearInterpolator = new LinearInterpolator();
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE:
                        //滑动停止
                        if (isScroll) {
                            isScroll = false;
                            scroll(choiceChild());
                        }
                        //矫正
                        if (isCheckScroll) {
                            isCheckScroll = false;
                            scroll(checkPosition);
                        }
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        //由手势触发的滑动开始
                        isScroll = true;
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING:
                        //不依靠外部输入的惯性滑动
                        break;
                    default:
                }
            }
        });
    }

    private int choiceChild() {
        int distance = 10000;
        int pos = 0;
        //选择显示最完整的一个item
        for (int i = 0; i < recyclerView.getChildCount(); i++) {
            View childAt = recyclerView.getChildAt(i);
            if (childAt != null) {
                ViewGroup parent = (ViewGroup) childAt.getParent();
                int bottom = parent.getHeight() - childAt.getTop() - childAt.getHeight();
                if (Math.abs(childAt.getTop() - bottom) < distance) {
                    distance = Math.abs(childAt.getTop() - bottom);
                    pos = i;
                }
            }
        }
        return pos + linearLayoutManager.findFirstVisibleItemPosition();
    }

    private void scroll(int pos) {
        if (pos == 0) {
            pos = 1;
        } else if (pos == recyclerView.getAdapter().getItemCount() - 1) {
            pos = recyclerView.getAdapter().getItemCount() - 2;
        }
        //对item滚动定位
        View childAt = recyclerView.getChildAt(pos - linearLayoutManager.findFirstVisibleItemPosition());
        if (childAt != null) {
            if (TYPE == TYPE_START) {
                recyclerView.smoothScrollBy(0, childAt.getTop(), linearInterpolator);
            } else {
                ViewGroup parent = (ViewGroup) childAt.getParent();
                int bottom = parent.getHeight() - childAt.getTop() - childAt.getHeight();
                if (TYPE == TYPE_CENTER) {
                    recyclerView.smoothScrollBy(0, (childAt.getTop() - bottom) / 2, linearInterpolator);
                } else if (TYPE == TYPE_END) {
                    recyclerView.smoothScrollBy(0, -bottom, linearInterpolator);
                }
            }
        }

        listener.stopSrcoll(pos);

        //监听回调，pos不同才回调
        if (position != pos) {
            position = pos;
            if (listener != null) {
                listener.change(position);
            }
        }
    }

    public void checkPosition(int pos) {
        //如果为-1表示recyclerView没有初始化完成
        if (linearLayoutManager.findFirstVisibleItemPosition() != -1) {
            if (pos < recyclerView.getAdapter().getItemCount() && checkPosition != pos) {
                checkPosition = pos;
                if (pos >= linearLayoutManager.findFirstVisibleItemPosition()
                        && pos <= linearLayoutManager.findLastVisibleItemPosition()) {
                    //在可视区域内
                    scroll(pos);
                } else {
                    //不在可视区域内，先移动至可视区域，然后在滑动监听中矫正
                    isCheckScroll = true;
                    recyclerView.smoothScrollToPosition(pos);
                }
            }
        }
    }

    public void setType(int TYPE) {
        this.TYPE = TYPE;
    }

    public void setListener(ScrollListener listener) {
        this.listener = listener;
    }

    public int getPosition() {
        return position;
    }

    public interface ScrollListener {
        void change(int position);

        void stopSrcoll(int position);
    }
}
